const baseUrl = getApp().globalData.baseUrl

/**
 * 统一调用接口
 */
function request(options) {
  return new Promise((resolve, reject) => {
    const {
      url,
      data,
      method
    } = options
    wx.showLoading({
      title: '加载中...',
      duration: 6000
    })
    wx.request({
      url: baseUrl + url,
      data: data || {},
      header: {
        'Content-Type': 'application/json;charset=UTF-8',
      },
      method: method || 'GET',
      dataType: 'json',
      responseType: 'text',
      success: res => {
        wx.hideLoading()
        let statusCode = res.statusCode
        console.log(statusCode);
        if (statusCode === 200) {
          resolve(res.data)
        } else {
          reject(res)
        }
      },
      fail: res => {
        wx.hideLoading()
        wx.showToast({
          title: '网络异常，请检查网络状态',
          icon: 'none',
          duration: 3000
        })
        reject(res)
      },
    })
  })
}

export default request;